package com.xnxy.design.action;

import com.xnxy.design.model.Announcement;
import com.xnxy.design.model.News;
import com.xnxy.design.service.CoreService;
import com.xnxy.design.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class NewsAction extends CoreController<News,Integer>{

    @Autowired
    NewsService newsService;

    @Override
    public CoreService<News, Integer> getService() {
        return newsService;
    }


    @RequestMapping("/news")
    public ModelAndView news(){
        List list=newsService.selectAll();
        ModelAndView modelAndView = new ModelAndView("news");
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @RequestMapping("/news-vide")
    public ModelAndView newsVide(Integer id){
        News news=newsService.selectByPrimaryKey(id);
        ModelAndView modelAndView = new ModelAndView("news-vide");
        modelAndView.addObject("news",news);
        return modelAndView;
    }


    @RequestMapping("/admin/news/delect")
    @ResponseBody
    public Map<String,Object> delectNews(Integer id[]){
        Boolean flag=delect(id);
        if (flag){
            return success(true,"删除成功");
        }else {
            return success(false,"删除失败");
        }

    }

    @RequestMapping("/admin/news/inster")
    @ResponseBody
    public Map<String,Object> instert(News news){
        news.setTime(new Date());
        if (news.getId()!=null){
            newsService.updata(news);
        }else {
            insert(news);
        }
        return success(true,"请求成功");
    }


    @RequestMapping("/admin/news/list")
    @ResponseBody
    private Map<String,Object> newsAll(){
        List list=selectAll();
        return success(true,"请求成功",list);
    }



    @RequestMapping("/admin/news/getNews")
    @ResponseBody
    public Map<String,Object> getNews(Integer id){
        News news=selectByPrimaryKey(id);
        return success(true,"请求成功",news);
    }
}
